﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using QD.Sht.Common;
using System.Data;
using System.Web;
using DaZhong.app_code;

namespace DaZhong
{
    public class DAOArticle
    {

        public static string Insert(List<MArticle> list)
        {
            try
            {
                int okCount = 0, noCount = 0;

                foreach (var t in list)
                {
                    if(string.IsNullOrWhiteSpace(t.content))continue;
                    t.title = HttpUtility.HtmlDecode(t.title);
                    if (Exist(t.title))
                    {
                        noCount++; continue;}
                    var id = Add(t);
                    if (id > 0)
                        okCount++;
                    else
                        noCount++;
                    AddDetail(t.listDetail, id);
                }
                return " 成功 " + okCount + " 重复 " + noCount;
            }
            catch (Exception e)
            {
                return e.Message;

            }
            
        }

        public static bool Exist(string title)
        {
           
            if (string.IsNullOrEmpty(title)) return false;
            SqlParameter[] paramCheck = { new SqlParameter("@title", title) };
            int isCount = 0;
            var count = SqlHelper2013.ExecuteScalar(System.Data.CommandType.Text, "select count(1) from vifo_article where state!=5 and title=@title ", paramCheck);
            int.TryParse(count.ToString(), out isCount);
            return isCount > 0;
        }

        private static int Add(MArticle model)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("insert into vifo_article(");
                strSql.Append("title,logo,content,state,date,source,sourceid,htmlurl,html_date,tag_id,tag_name,read_num,like_num)");
                strSql.Append(" values (");
                strSql.Append("@title,@logo,@content,@state,@date,@source,@sourceid,@htmlurl,@html_date,@tag_id,@tag_name,@read_num,@like_num)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters = {
                    new SqlParameter("@title", SqlDbType.NVarChar,200),
                    
                    new SqlParameter("@logo", SqlDbType.VarChar,200),
                    new SqlParameter("@content", SqlDbType.NVarChar),
                   
                    new SqlParameter("@state", SqlDbType.Int,4),
                    new SqlParameter("@date", SqlDbType.DateTime),
                    new SqlParameter("@source", SqlDbType.VarChar,50),
                    new SqlParameter("@sourceid", SqlDbType.Int,4),
                    new SqlParameter("@htmlurl", SqlDbType.VarChar,300),
                    new SqlParameter("@html_date", SqlDbType.VarChar,50),
                    new SqlParameter("@tag_id",SqlDbType.Int),
                    new SqlParameter("@tag_name",SqlDbType.VarChar),
                    new SqlParameter("@read_num",SqlDbType.Int),
                    new SqlParameter("@like_num",SqlDbType.Int)
                                        };
                parameters[0].Value = model.title ?? "";

                parameters[1].Value = model.logo ?? "";
                parameters[2].Value = model.content ?? "";
               
                parameters[3].Value = model.state;
                parameters[4].Value = DateTime.Now;
                parameters[5].Value = model.source ?? "";
                parameters[6].Value = model.sourceid;
                parameters[7].Value = model.htmlurl ?? "";
                parameters[8].Value = model.html_date ?? "";
                parameters[9].Value = model.Tag_Id;
                parameters[10].Value = model.Tag_Name ?? "";
                parameters[11].Value = model.ReadNum;
                parameters[12].Value = model.LikeNum;
                var obj = SqlHelper2013.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);
                return !string.IsNullOrEmpty(obj.ToString()) ? Convert.ToInt32(obj) : 0;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            
        }

        static void AddDetail(List<MArticleDetail> list, int article_id)
        {
            return;

            if (list == null || list.Count < 1)
                return;
            for (int i = 0; i < list.Count; i++)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into article_detail(");
                strSql.Append("article_id,isFirst,page,content,date)");
                strSql.Append(" values (");
                strSql.Append("@article_id,@isFirst,@page,@content,@date)");
                SqlParameter[] parameters = {
					new SqlParameter("@article_id", SqlDbType.Int,4),
					new SqlParameter("@isFirst", SqlDbType.Int,4),
					new SqlParameter("@page", SqlDbType.Int,4),
					new SqlParameter("@content", SqlDbType.VarChar,int.MaxValue),
					new SqlParameter("@date", SqlDbType.DateTime)};
                parameters[0].Value = article_id;
                parameters[1].Value = list[i].isFirst;
                parameters[2].Value = list[i].page;
                parameters[3].Value = list[i].context == null ? "" : list[i].context;
                parameters[4].Value = DateTime.Now;

                int count = SqlHelper2013.ExecuteNonQuery(strSql.ToString(), parameters);
            }
        }
    }
}
